generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider  = "postgresql"
  url       = env("POSTGRES_URL")
  directUrl = env("POSTGRES_URL_NON_POOLING")
}

model User {
  id Int @id @default(autoincrement())

  fullName String
  email    String   @unique
  password String
  role     UserRole @default(USER)
  verified DateTime

  provider   String?
  providerId String?

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  cart              Cart?
  orders            Order[]
  verificationCodes VerificationCode?
}

// Product
model BusinessObject {
  id Int @id @default(autoincrement())

  name     String
  imageUrl String

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  category   Category @relation(fields: [categoryId], references: [id])
  categoryId Int

  extraCategories ExtraCategory[]

  variations BOvariable[]
}

// ProductItem
model BOvariable {
  id Int @id @default(autoincrement())

  price Int
  size  Int?
  type  Int?

  cartItems CartItem[]

  buinessObject   BusinessObject @relation(fields: [buinessObjectId], references: [id])
  buinessObjectId Int
}

// Ingredient
model Category {
  id   Int    @id @default(autoincrement())
  name String @unique

  buinessObject BusinessObject[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model ExtraCategory {
  id Int @id @default(autoincrement())

  name     String
  price    Int
  imageUrl String

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  buinessObject BusinessObject[]
  cartItems     CartItem[]
}

model Cart {
  id Int @id @default(autoincrement())

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  token String

  items CartItem[]

  user   User? @relation(fields: [userId], references: [id])
  userId Int?  @unique

  totalAmount Int @default(0)
}

model CartItem {
  id Int @id @default(autoincrement())

  cart   Cart @relation(fields: [cartId], references: [id])
  cartId Int

  boVariable   BOvariable @relation(fields: [boVariableId], references: [id])
  boVariableId Int

  quantity Int @default(1)

  extraCategory ExtraCategory[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

enum OrderStatus {
  PENDING
  COMPLETED
  CANCELLED
}

model Order {
  id Int @id @default(autoincrement())

  user   User? @relation(fields: [userId], references: [id])
  userId Int?

  token String

  totalAmount Int
  status      OrderStatus
  paynemtId   String?

  items Json

  fullName String
  email    String
  address  String
  phone    String
  comment  String?

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model VerificationCode {
  id Int @id @default(autoincrement())

  user   User @relation(fields: [userId], references: [id])
  userId Int  @unique

  code String

  createdAt DateTime @default(now())

  @@unique([userId, code])
}

enum UserRole {
  USER
  ADMIN
}
